package com.woniuxy.store.core.controller;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.common.CommonResult;
import com.woniuxy.store.core.model.PlAddress;
import com.woniuxy.store.core.model.PlOrderitem;
import com.woniuxy.store.core.service.PlOrderService;
import com.woniuxy.store.core.service.PlOrderitemService;
import com.woniuxy.store.core.vo.IncomeVo;
import com.woniuxy.store.core.vo.OrderItemVo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author gmx
 * @since 2021-07-05
 */
@RestController
@RequestMapping("/plOrderitem")
public class PlOrderitemController {
    @Resource
    PlOrderitemService orderitemService;

    /**
     * Description : 商家查询自己门店的订单列表及每个订单的详情数据。
     * ChangeLog : 1. 创建 (2021/7/5 14:39 [fulang]);
     *
     * @param storeId
     * @return com.woniuxy.common.CommonResult<java.util.List < com.woniuxy.store.core.model.PlOrderitem>>
     */
//    @GetMapping("selectAll/{storeId}")
//    public CommonResult<List<OrderItemVo>> selectAll(@PathVariable Integer storeId){
//        List<OrderItemVo> plOrderitems = orderitemService.selectAll(storeId);
//        List<OrderItemVo> chinese = chinese(plOrderitems);
//        return CommonResult.success(chinese);
//    }
    /**
     * Description : 商家查询自己门店的订单列表及每个订单的详情数据。
     * ChangeLog : 1. 创建 (2021/7/8 9:35 [fulang]);
     * @param storeId
     * @param startpage
     * @param pagesize
     * @return com.woniuxy.common.CommonResult<com.github.pagehelper.PageInfo<com.woniuxy.store.core.vo.OrderItemVo>>
    */
    @GetMapping("selectAll/{storeId}/{startpage}/{pagesize}")
    public CommonResult<PageInfo<OrderItemVo>> selectAll(@PathVariable Integer storeId, @PathVariable int startpage, @PathVariable int pagesize) {
        PageHelper.startPage(startpage, pagesize);
        List<OrderItemVo> orderItemVos = orderitemService.selectAll(storeId);
        List<OrderItemVo> chinese = chinese(orderItemVos);
        Page<OrderItemVo> page = (Page<OrderItemVo>) chinese;
        PageInfo<OrderItemVo> info = new PageInfo<>(page);
        return CommonResult.success(info);
    }
    /**
     * Description : 生成订单类型,状态中文
     * ChangeLog : 1. 创建 (2021/7/8 9:36 [fulang]);
     * @param orderItemVos
     * @return java.util.List<com.woniuxy.store.core.vo.OrderItemVo>
    */
    public List<OrderItemVo> chinese(List<OrderItemVo> orderItemVos) {
        for (OrderItemVo orderItem : orderItemVos
        ) {
            if (orderItem.getStatus() == 0) {
                orderItem.setStatusch("待付款");
            }
            if (orderItem.getStatus() == 1) {
                orderItem.setStatusch("待收货");
            }
            if (orderItem.getStatus() == 2) {
                orderItem.setStatusch("待服务");
            }
            if (orderItem.getStatus() == 3) {
                orderItem.setStatusch("待评价");
            }
            if (orderItem.getStatus() == 4) {
                orderItem.setStatusch("待售后");
            }
            if (orderItem.getStatus() == 5) {
                orderItem.setStatusch("已完成");
            }
            if (orderItem.getOrdertype() == 0) {
                orderItem.setOrdertypech("商品");
            } else {
                orderItem.setOrdertypech("服务");
            }

        }
        return orderItemVos;
    }

    /**
     * Description : 服务项目的收益数据统计  <br/>
     * ChangeLog : 1. 创建 (2021/7/5 19:50 [binhu]);
     *
     * @param storeId
     * @return com.woniuxy.common.CommonResult
     */
    @GetMapping("/incomeStatistics")
    public CommonResult incomeStatistics(Integer storeId) {
        List<OrderItemVo> orderitems = orderitemService.selectOrderItemByStoreId(storeId);

        BigDecimal income = new BigDecimal("0.0");
        for (OrderItemVo orderitem : orderitems) {
            BigDecimal sellPrice = orderitem.getSellPrice();
            income = income.add(sellPrice);
        }
        return CommonResult.success(income);
    }

    /**
     * Description : 服务项目的具体收益及平台抽成  <br/>
     * ChangeLog : 1. 创建 (2021/7/5 20:21 [binhu]);
     *
     * @param storeId
     * @param firstId
     * @return com.woniuxy.common.CommonResult
     */
    @GetMapping("/specificIncomeAndPlatformCommission")
    public CommonResult specificIncomeAndPlatformCommission(Integer storeId, Integer firstId) {
        List<OrderItemVo> orderitems = orderitemService.selectByStoreIdFirstId(storeId, firstId);
        BigDecimal specificIncome = new BigDecimal("0.0");
        BigDecimal platformCommission = new BigDecimal("0.0");
        //平台抽成比例
        BigDecimal fraction = new BigDecimal("0.01");
        for (OrderItemVo orderitem : orderitems) {
            BigDecimal sellPrice = orderitem.getSellPrice();
            specificIncome = specificIncome.add(sellPrice);
            platformCommission = platformCommission.add(sellPrice.multiply(fraction));
        }
        IncomeVo incomeVo = new IncomeVo();
        incomeVo.setPlatformCommission(platformCommission);
        incomeVo.setSpecificIncome(specificIncome);
        return CommonResult.success(incomeVo);
    }

    /**
     * Description : 提现  <br/>
     * ChangeLog : 1. 创建 (2021/7/6 9:41 [binhu]);
     *
     * @param storeId
     * @return com.woniuxy.common.CommonResult
     */
    @GetMapping("/withdraw")
    public CommonResult withdraw(Integer storeId) {
        List<OrderItemVo> listCommonResult = orderitemService.selectAll(storeId);

        BigDecimal totalIncome = new BigDecimal("0.0");
        for (OrderItemVo orderitem : listCommonResult) {
            BigDecimal sellPrice = orderitem.getSellPrice();
            totalIncome = totalIncome.add(sellPrice);
        }
        //提成规则
        BigDecimal commissionRules = new BigDecimal("0.5");
        BigDecimal withdraw = totalIncome.multiply(commissionRules);
        return CommonResult.success(withdraw);
    }


    /**
     * Description : 商家查询自己门店未付款的订单
     * ChangeLog : 1. 创建 (2021/7/6 10:36 [fulang]);
     *
     * @param storeId
     * @return com.woniuxy.common.CommonResult<java.util.List < com.woniuxy.store.core.model.PlOrderitem>>
     */
    @GetMapping("selectUnpaid/{storeId}/{startpage}/{pagesize}")
    public CommonResult<PageInfo<OrderItemVo>> selectUnpaid(@PathVariable Integer storeId, @PathVariable int startpage, @PathVariable int pagesize) {
        PageHelper.startPage(startpage, pagesize);
        List<OrderItemVo> plOrderitems = orderitemService.selectUnpaid(storeId);
        List<OrderItemVo> chinese = chinese(plOrderitems);
        Page<OrderItemVo> page = (Page<OrderItemVo>) chinese;
        PageInfo<OrderItemVo> info = new PageInfo<>(page);
        return CommonResult.success(info);
    }

    /**
     * Description : 商家查询自己门店未收货的订单
     * ChangeLog : 1. 创建 (2021/7/6 10:38 [fulang]);
     *
     * @param storeId
     * @return com.woniuxy.common.CommonResult<java.util.List < com.woniuxy.store.core.model.PlOrderitem>>
     */
    @GetMapping("selectUnChecked/{storeId}/{startpage}/{pagesize}")
    public CommonResult<PageInfo<OrderItemVo>> selectUnChecked(@PathVariable Integer storeId, @PathVariable int startpage, @PathVariable int pagesize) {
        PageHelper.startPage(startpage, pagesize);
        List<OrderItemVo> plOrderitems = orderitemService.selectUnChecked(storeId);
        List<OrderItemVo> chinese = chinese(plOrderitems);
        Page<OrderItemVo> page = (Page<OrderItemVo>) chinese;
        PageInfo<OrderItemVo> info = new PageInfo<>(page);
        return CommonResult.success(info);
    }

    /**
     * Description : 商家查询自己门店待服务的订单
     * ChangeLog : 1. 创建 (2021/7/6 10:39 [fulang]);
     *
     * @param storeId
     * @return com.woniuxy.common.CommonResult<java.util.List < com.woniuxy.store.core.model.PlOrderitem>>
     */
    @GetMapping("selectUnServiced/{storeId}/{startpage}/{pagesize}")
    public CommonResult<PageInfo<OrderItemVo>> selectUnServiced(@PathVariable Integer storeId, @PathVariable int startpage, @PathVariable int pagesize) {
        PageHelper.startPage(startpage, pagesize);
        List<OrderItemVo> plOrderitems = orderitemService.selectUnServiced(storeId);
        List<OrderItemVo> chinese = chinese(plOrderitems);
        Page<OrderItemVo> page = (Page<OrderItemVo>) chinese;
        PageInfo<OrderItemVo> info = new PageInfo<>(page);
        return CommonResult.success(info);
    }

    /**
     * Description : 商家查询自己门店待评价的订单  <br/>
     * ChangeLog : 1. 创建 (2021/7/6 10:40 [fulang]);
     *
     * @param storeId
     * @return com.woniuxy.common.CommonResult<java.util.List < com.woniuxy.store.core.model.PlOrderitem>>
     */
    @GetMapping("selectUnEvaluate/{storeId}/{startpage}/{pagesize}")
    public CommonResult<PageInfo<OrderItemVo>> selectUnEvaluate(@PathVariable Integer storeId, @PathVariable int startpage, @PathVariable int pagesize) {
        PageHelper.startPage(startpage, pagesize);
        List<OrderItemVo> plOrderitems = orderitemService.selectUnEvaluate(storeId);
        List<OrderItemVo> chinese = chinese(plOrderitems);
        Page<OrderItemVo> page = (Page<OrderItemVo>) chinese;
        PageInfo<OrderItemVo> info = new PageInfo<>(page);
        return CommonResult.success(info);
    }

    /**
     * Description : 商家查询自己门店待售后服务的订单
     * ChangeLog : 1. 创建 (2021/7/6 10:40 [fulang]);
     *
     * @param storeId
     * @return com.woniuxy.common.CommonResult<java.util.List < com.woniuxy.store.core.model.PlOrderitem>>
     */
    @GetMapping("selectUnAfterSale/{storeId}/{startpage}/{pagesize}")
    public CommonResult<PageInfo<OrderItemVo>> selectUnAfterSale(@PathVariable Integer storeId, @PathVariable int startpage, @PathVariable int pagesize) {
        PageHelper.startPage(startpage, pagesize);
        List<OrderItemVo> plOrderitems = orderitemService.selectUnAfterSale(storeId);
        List<OrderItemVo> chinese = chinese(plOrderitems);
        Page<OrderItemVo> page = (Page<OrderItemVo>) chinese;
        PageInfo<OrderItemVo> info = new PageInfo<>(page);
        return CommonResult.success(info);
    }

    /**
     * Description :商家查询自己门店已完成的订单
     * ChangeLog : 1. 创建 (2021/7/6 10:41 [fulang]);
     *
     * @param storeId
     * @return com.woniuxy.common.CommonResult<java.util.List < com.woniuxy.store.core.model.PlOrderitem>>
     */
    @GetMapping("selectFinished/{storeId}/{startpage}/{pagesize}")
    public CommonResult<PageInfo<OrderItemVo>> selectFinished(@PathVariable Integer storeId, @PathVariable int startpage, @PathVariable int pagesize) {
        PageHelper.startPage(startpage, pagesize);
        List<OrderItemVo> plOrderitems = orderitemService.selectFinished(storeId);
        List<OrderItemVo> chinese = chinese(plOrderitems);
        Page<OrderItemVo> page = (Page<OrderItemVo>) chinese;
        PageInfo<OrderItemVo> info = new PageInfo<>(page);
        return CommonResult.success(info);
    }

    /**
     * Description : 商家查询当日的订单
     * ChangeLog : 1. 创建 (2021/7/6 10:41 [fulang]);
     *
     * @param storeId
     * @return com.woniuxy.common.CommonResult<java.util.List < com.woniuxy.store.core.model.PlOrderitem>>
     */
    @GetMapping("selectDay/{storeId}/{startpage}/{pagesize}")
    public CommonResult<PageInfo<OrderItemVo>> selectDay(@PathVariable Integer storeId, @PathVariable int startpage, @PathVariable int pagesize) {
        PageHelper.startPage(startpage, pagesize);
        List<OrderItemVo> plOrderitems = orderitemService.selectDay(storeId);
        List<OrderItemVo> chinese = chinese(plOrderitems);
        Page<OrderItemVo> page = (Page<OrderItemVo>) chinese;
        PageInfo<OrderItemVo> info = new PageInfo<>(page);
        return CommonResult.success(info);
    }

    /**
     * Description : 商家查询当周的订单
     * ChangeLog : 1. 创建 (2021/7/6 10:42 [fulang]);
     *
     * @param storeId
     * @return com.woniuxy.common.CommonResult<java.util.List < com.woniuxy.store.core.model.PlOrderitem>>
     */
    @GetMapping("selectWeek/{storeId}/{startpage}/{pagesize}")
    public CommonResult<PageInfo<OrderItemVo>> selectWeek(@PathVariable Integer storeId, @PathVariable int startpage, @PathVariable int pagesize) {
        PageHelper.startPage(startpage, pagesize);
        List<OrderItemVo> plOrderitems = orderitemService.selectWeek(storeId);
        List<OrderItemVo> chinese = chinese(plOrderitems);
        Page<OrderItemVo> page = (Page<OrderItemVo>) chinese;
        PageInfo<OrderItemVo> info = new PageInfo<>(page);
        return CommonResult.success(info);
    }

    /**
     * Description : 商家查询当月的订单
     * ChangeLog : 1. 创建 (2021/7/6 10:43 [fulang]);
     *
     * @param storeId
     * @return com.woniuxy.common.CommonResult<java.util.List < com.woniuxy.store.core.model.PlOrderitem>>
     */
    @GetMapping("selectMonth/{storeId}/{startpage}/{pagesize}")
    public CommonResult<PageInfo<OrderItemVo>> selectMonth(@PathVariable Integer storeId, @PathVariable int startpage, @PathVariable int pagesize) {
        PageHelper.startPage(startpage, pagesize);
        List<OrderItemVo> plOrderitems = orderitemService.selectMonth(storeId);
        List<OrderItemVo> chinese = chinese(plOrderitems);
        Page<OrderItemVo> page = (Page<OrderItemVo>) chinese;
        PageInfo<OrderItemVo> info = new PageInfo<>(page);
        return CommonResult.success(info);
    }

    /**
     * Description : 商家查询当前季度的订单
     * ChangeLog : 1. 创建 (2021/7/6 10:43 [fulang]);
     *
     * @param storeId
     * @return com.woniuxy.common.CommonResult<java.util.List < com.woniuxy.store.core.model.PlOrderitem>>
     */
    @GetMapping("selectSeason/{storeId}/{startpage}/{pagesize}")
    public CommonResult<PageInfo<OrderItemVo>> selectSeason(@PathVariable Integer storeId, @PathVariable int startpage, @PathVariable int pagesize) {
        PageHelper.startPage(startpage, pagesize);
        List<OrderItemVo> plOrderitems = orderitemService.selectSeason(storeId);
        List<OrderItemVo> chinese = chinese(plOrderitems);
        Page<OrderItemVo> page = (Page<OrderItemVo>) chinese;
        PageInfo<OrderItemVo> info = new PageInfo<>(page);
        return CommonResult.success(info);
    }
    /**
     * Description : 查询地址
     * ChangeLog : 1. 创建 (2021/7/9 10:58 [fulang]);
     * @param orderitemId
     * @return java.util.List<com.woniuxy.store.core.model.PlAddress>
    */
    @GetMapping("selectAddress/{orderitemId}")
    public List<PlAddress> selectAddress(@PathVariable Integer orderitemId){
        List<PlAddress> plAddresses = orderitemService.selectAddress(orderitemId);
        return plAddresses;
    }
}

